package com.example.hcd.tool;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.hcd.R;
import com.example.hcd.datamanage.UserInfoData;
import com.example.hcd.internet.InternetKeep;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Dell on 2016/8/12.
 */
public class SelectPersonPop extends PopupWindow {


    private Context context;
    private List<UserInfoData> userInfoList;

    ListAdapter listAdapter;
    private View view;
    private AdapterView.OnItemClickListener listener;
    public SelectPersonPop(Context context, View view, AdapterView.OnItemClickListener listener)
    {
        super(context);
        this.context =context;
        this.view =view;
        this.listener =listener;
        userInfoList = InternetKeep.getInstence().getUserinfolist();
        setContentView(LayoutInflater.from(context).inflate(R.layout.select_person_view, null));
    }

    public void filterWords(CharSequence s)
    {
        listAdapter.getFilter().filter(s);
    }


    @Override
    public void setContentView(View contentView) {
        super.setContentView(contentView);
        init();
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setBackgroundDrawable(new BitmapDrawable());
        setOutsideTouchable(true);
    }

    private void init() {

        ListView listView =(ListView) getContentView().findViewById(R.id.listView);
        listView.setOnItemClickListener(listener);
        listAdapter =new ListAdapter(userInfoList);
        listView.setAdapter(listAdapter);
    }


    private class ListAdapter extends BaseAdapter {
        private PersonFilter filter;

        private List<UserInfoData> data;
        private ListAdapter(List<UserInfoData> data)
        {
            this.data =data;
        }

        @Override
        public int getCount() {
            if(data!=null&&data.size()>0) {
                return data.size();
            }else
            {
                return 0;
            }
        }

        @Override
        public UserInfoData getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView =LayoutInflater.from(context).inflate(R.layout.selete_person_item, null);
            TextView textView =(TextView)convertView.findViewById(R.id.name_text);
            textView.setText(data.get(position).name);
            return convertView;
        }

        public Filter getFilter() {
            if (filter == null) {
                filter = new PersonFilter(userInfoList);
            }
            return filter;
        }

        private class PersonFilter extends Filter {

            private List<UserInfoData> original;

            public PersonFilter(List<UserInfoData> list) {
                this.original = list;
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
//                if (constraint == null || constraint.length() == 0) {
//                    results.values = original;
//                    results.count = original.size();
//                } else {
                    List<UserInfoData> mList = new ArrayList<UserInfoData>();
                    for (UserInfoData p: original) {
                        if (constraint!=null&&p.name.contains(constraint.toString())&&constraint.length()!=0) {
                            mList.add(p);
                        }
                    }
                    results.values = mList;
                    results.count = mList.size();
//                }
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint,
                                          FilterResults results) {
                data = (List<UserInfoData>)results.values;
                notifyDataSetChanged();
                if(data!=null)
                Log.e("size",data.size()+"");
                if(data!=null&&data.size()>0)
                {
                    SelectPersonPop.this.showAsDropDown(view);
                }else
                {
                    SelectPersonPop.this.dismiss();
                }
            }

        }

    }





}
